package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.ResultModel;
import com.example.demo.entity.User;
//import com.example.demo.group.Group_1;
//import com.example.demo.group.Group_2;
import com.example.demo.service.UserService;
import com.example.demo.mapper.UserMapper;
import lombok.AllArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController           //@RestController注解相当于@ResponseBody ＋ @Controller
@AllArgsConstructor        //lombok中的注解,作用在类上;使用后添加一个构造函数，该构造函数含有所有已声明字段属性参数
@RequestMapping("user")
public class UserController {

    private final UserMapper userMapper;

    private final UserService userService;

//    @PutMapping("/insert")
//    public ResultModel save(@RequestBody User user) {
////        if(bindingResult.hasErrors()){
////            return bindingResult.getFieldError().toString();
////        }
//        Integer count = userMapper.save(user);
//        return ResultModel.ok(String.format("插入成功，返回受影响行数 %d",count));
//    }

    @PutMapping("insert")     //对应修改操作，表明是一个修改URL映射
    public ResultModel add(User user){
        Integer count = userMapper.save(user);
        return ResultModel.ok(String.format("插入成功，返回受影响行数 %d",count));
    }

    @GetMapping("list")       //Get的请求方式
    public ResultModel list() {
        return ResultModel.ok(userMapper.selectUsers());
    }

    @DeleteMapping("delete/{id}")   //对应删除，表明是一个删除URL映射”
    public ResultModel delete(@PathVariable Integer id) {
        return ResultModel.ok(String.format("删除成功，受影响行数为 %d", userMapper.deleteUser(id)));
    }

    @GetMapping("get/{id}")
    public ResultModel get(@PathVariable Integer id) {
        return ResultModel.ok(userMapper.getUser(id));
    }

    @PostMapping("update")      //Post的请求方式
    public ResultModel update(User user) {
        userMapper.updateUser(user);
        return ResultModel.ok("修改成功");
    }

//    @GetMapping("page")
//    private PageInfo<User> getPageInfo(@RequestParam(defaultValue = "0") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
//        PageHelper.startPage(pageNum, pageSize);
//        List<User> users = userMapper.selectUsers();
//        return new PageInfo<>(users);
//    }
    @GetMapping("getAll")
    private List<User> getAll(){
        return userService.list(new LambdaQueryWrapper<User>().eq(true,User::getUserName,"lisi").select(User::getNickName,User::getAge));
    }

    @GetMapping("getByPage")
    public ResultModel getByPage(Page page,String nick){
        IPage list = userService.page(page,new QueryWrapper<User>().like(nick != null,"nick_name",nick));
        return ResultModel.ok(list.getRecords(),list.getTotal());
    }
}
